package com.kailong.dao.student;

import com.kailong.dao.BaseDao;
import com.kailong.pojo.Seat;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class studentSeatDao {
    public static ArrayList<Seat> selectAll(int cpage, int count, String floor, String square) {
        ArrayList<Seat> list = new ArrayList<>();
        //声明结果集
        ResultSet rs = null;
        //获取连接对象
        Connection conn = BaseDao.getconn();

        PreparedStatement ps = null;
        try {
            String sql = "";
            if (floor != null && square != null) {
                sql = "select * from seat where tfloor = ? and tsquare = ? tstate = ? order by tno  limit ?,?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, floor);
                ps.setString(2, square);
                ps.setString(3, "未预约");
                ps.setInt(4, (cpage - 1) * count);
                ps.setInt(5, count);
            } else {
                sql = "select * from seat order by tno desc limit ?,?";
                ps = conn.prepareStatement(sql);

                ps.setInt(1, (cpage - 1) * count);
                ps.setInt(2, count);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                Seat s = new Seat(
                        rs.getInt("tno"),
                        rs.getString("tfloor"),
                        rs.getString("tsquare"),
                        rs.getString("tstate"),
                        rs.getString("tinfo")
                );
                list.add(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeall(rs, ps, conn);
        }
        return  list;
    }
}
